package com.ele.dao.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;
import com.ele.dao.ShopDao;
import com.ele.pojo.Shop;
import com.ele.utils.JDBCUtils;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ShopDaoImpl implements ShopDao {
    private DataSource datasource = JDBCUtils.getDatasource();
    @Override
    public Integer shopAdd(Shop shop) throws Exception {

        return Db.use(datasource).insert(Entity.create("shop")
                .set("shop_name", shop.getShopName())
                .set("shop_address", shop.getShopAddress())
                .set("shop_explain", shop.getShopExplain())
                .set("shop_img", shop.getShopImg())
                .set("shop_category_id", shop.getShopCategoryId())
                .set("start_price", shop.getStartPrice())
                .set("delivery_price", shop.getDeliveryPrice())
                .set("status", 0)
                .set("start_time", shop.getStartTime())
                .set("end_time", shop.getEndTime())
                .set("recommend", 0)
                .set("business_id", shop.getBusinessId()));
    }
    public List<Shop> shopList(String currentPage, String pageSize, String businessId) throws SQLException {
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if( StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        page = (page - 1) * size;
        return Db.use(datasource).query("select * from shop where business_id = ? limit ? , ? ", Shop.class , businessId, page , size);
    }

    public Long selectCount(String businessId) throws SQLException {
        return Db.use(datasource).count(Entity.create("shop").set("business_id",businessId));
    }

    public List<Shop> shopAuditList(String currentPage, String pageSize, String shopName, String status) throws SQLException {
        String sql = "select * from shop ";
        Map<String ,String> map = new HashMap<String ,String>();
        if(StrUtil.isNotBlank(shopName)){
            map.put("shop_name",shopName);
        }
        if(StrUtil.isNotBlank(status)){
            map.put("status",status);
        }
        if(!map.isEmpty()){
            sql = sql + " where ";
            Set<Map.Entry<String, String>> entrySet = map.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                if(entry.getKey().equals("status")){
                    sql = sql + entry.getKey() +" = " + entry.getValue() + " or ";
                }else{
                    sql = sql + entry.getKey() +" like '%"+entry.getValue()+"%' or ";
                }
            }
            sql = sql.substring(0,sql.lastIndexOf("or"));
        }
        Integer page = 1;
        Integer size = 2;
        if(StrUtil.isNotBlank(currentPage)){
            page = Integer.valueOf(currentPage);
        }
        if( StrUtil.isNotBlank(pageSize)){
            size = Integer.parseInt(pageSize);
        }
        sql = sql + "limit "+(page - 1) * size +" , " + size;
        System.out.println(sql);
        return Db.use(datasource).query(sql, Shop.class);
    }

    public Long selectCount() throws SQLException {
        return Db.use(datasource).count(Entity.create("shop"));
    }

    @Override
    public Shop findShopById(Integer sId) throws SQLException {
        return Db.use(datasource).query("select * from shop where shop_id = ?" , BeanHandler.create(Shop.class),sId);
    }

    @Override
    public Integer changeStatus(String shopId, String status) throws SQLException {
        return Db.use(datasource).update(Entity.create().set("status",status),Entity.create("shop").set("shop_id",shopId));
    }
}
